/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.modelo.MaiusculaseMinusculaseEspaco;
import br.com.evl.tecinfo.modelo.TudoMenosEspeciais;
import br.com.evl.tecinfo.modelo.ApenasLetrasMaiusculaseMinuscula;
import br.com.evl.tecinfo.modelo.ApenasLetrasMinusculas;
import br.com.evl.tecinfo.modelo.Usuario;
import java.awt.Color;
import java.awt.Font;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Jalisson
 */
public class CadastroUsuario extends javax.swing.JFrame {

    private int clicou_ok;
    private int linha_editar = -1;
    private ArrayList<JCheckBox> areasJCheckBox = new ArrayList();

    /**
     * Creates new form Usuario
     */
    public CadastroUsuario() {
        initComponents();
        setSize(400, 400);
        setLocationRelativeTo(this);
        setTitle("Cadastrar Usuário");
        
        

        //LIMITANDO CARACTERES DO FORMULÁRIO
        //
        JTextField textFieldNome = new JTextField();
        campoNome.setDocument(new MaiusculaseMinusculaseEspaco(50));

        JTextField textFieldLogin = new JTextField();
        jFormattedTextField_Login.setDocument(new ApenasLetrasMinusculas(20));


        ImageIcon icone = new ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/addusuario.png"));
        setIconImage(icone.getImage());



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelPermissao = new javax.swing.JLabel();
        jLabelLogin = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        textocadastro = new javax.swing.JLabel();
        jLabelSenha = new javax.swing.JLabel();
        campoNome = new javax.swing.JFormattedTextField();
        jFormattedTextField_Login = new javax.swing.JFormattedTextField();
        jPasswordFieldSenhaUsuario = new javax.swing.JPasswordField();
        jButtonCadastro = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabelIMGLogo = new javax.swing.JLabel();
        jComboBox_Cargo = new javax.swing.JComboBox();
        jLabelIMGCadastrarUsuario = new javax.swing.JLabel();
        jLabelIMGBacgroundUsuario = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(null);

        jLabelPermissao.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabelPermissao.setText("Permissão:");
        getContentPane().add(jLabelPermissao);
        jLabelPermissao.setBounds(20, 240, 70, 30);

        jLabelLogin.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabelLogin.setText("Login:");
        getContentPane().add(jLabelLogin);
        jLabelLogin.setBounds(20, 160, 60, 30);

        jLabelNome.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabelNome.setText("Nome:");
        getContentPane().add(jLabelNome);
        jLabelNome.setBounds(20, 120, 60, 30);

        textocadastro.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        textocadastro.setForeground(new java.awt.Color(102, 102, 102));
        textocadastro.setText("CADASTRAR USUÁRIO");
        getContentPane().add(textocadastro);
        textocadastro.setBounds(160, 50, 160, 32);

        jLabelSenha.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabelSenha.setText("Senha:");
        getContentPane().add(jLabelSenha);
        jLabelSenha.setBounds(20, 200, 60, 30);

        campoNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoNomeActionPerformed(evt);
            }
        });
        getContentPane().add(campoNome);
        campoNome.setBounds(90, 120, 270, 30);
        getContentPane().add(jFormattedTextField_Login);
        jFormattedTextField_Login.setBounds(90, 160, 270, 30);
        getContentPane().add(jPasswordFieldSenhaUsuario);
        jPasswordFieldSenhaUsuario.setBounds(90, 200, 270, 30);

        jButtonCadastro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/conformado20x20.png"))); // NOI18N
        jButtonCadastro.setText("CADASTRAR");
        jButtonCadastro.setToolTipText("");
        jButtonCadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCadastroActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonCadastro);
        jButtonCadastro.setBounds(60, 310, 130, 30);

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/sair20x20.png"))); // NOI18N
        jButtonCancelar.setText("CANCELAR");
        jButtonCancelar.setToolTipText("");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonCancelar);
        jButtonCancelar.setBounds(210, 310, 130, 30);

        jLabelIMGLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/addusuario.png"))); // NOI18N
        getContentPane().add(jLabelIMGLogo);
        jLabelIMGLogo.setBounds(40, 20, 70, 80);

        jComboBox_Cargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "nenhum", "Assitente", "Coordenador", "Presidente", "Professor" }));
        getContentPane().add(jComboBox_Cargo);
        jComboBox_Cargo.setBounds(90, 240, 270, 30);

        jLabelIMGCadastrarUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/CADASTRODEUSUARIO.png"))); // NOI18N
        getContentPane().add(jLabelIMGCadastrarUsuario);
        jLabelIMGCadastrarUsuario.setBounds(140, 40, 200, 50);

        jLabelIMGBacgroundUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/backgroud.jpg"))); // NOI18N
        jLabelIMGBacgroundUsuario.setText("jLabel1");
        getContentPane().add(jLabelIMGBacgroundUsuario);
        jLabelIMGBacgroundUsuario.setBounds(-6, -10, 400, 390);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        clicou_ok = JOptionPane.showConfirmDialog(null, "Deseja cancelar o cadastramento do usuário?", "Confirmação", JOptionPane.YES_NO_OPTION);
        if (clicou_ok == JOptionPane.YES_OPTION) {
            this.dispose();
        } else if (clicou_ok == JOptionPane.NO_OPTION) {
        }
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonCadastroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCadastroActionPerformed
        ArrayList<Usuario> usuarios = new ArrayList<>();
        Usuario nUsuario = new Usuario();
        nUsuario.setNome(campoNome.getText());


        usuarios.add(nUsuario);

        nUsuario.setLogin(jFormattedTextField_Login.getText());
        nUsuario.setSenha(jPasswordFieldSenhaUsuario.getText());
        usuarios.add(nUsuario);

        boolean nomevazio = false;
        boolean areavazio = false;
        boolean loginavazio = false;
        boolean senhavazio = false;
        boolean permissaovazio = false;
        Color corpermissao = jLabelPermissao.getForeground();
        //CÓDIGO PARA CHECAR SE OS CAMPOS DO CADASTROS E ALTERAR COR DOS CAMPOS VAZIOS
        if (linha_editar == -1) {
            if (campoNome.getText().equals("")) {
                campoNome.setBackground(new Color(255, 209, 209));
                nomevazio = true;
            } else {
                campoNome.setBackground(new Color(255, 255, 255));
                nomevazio = false;
            }
            if (jFormattedTextField_Login.getText().equals("")) {
                jFormattedTextField_Login.setBackground(new Color(255, 209, 209));
                loginavazio = true;
            } else {
                jFormattedTextField_Login.setBackground(new Color(255, 255, 255));
                loginavazio = false;
            }
            if (jPasswordFieldSenhaUsuario.getPassword().length == 0) {
                jPasswordFieldSenhaUsuario.setBackground(new Color(255, 209, 209));
                senhavazio = true;
            } else {
                jPasswordFieldSenhaUsuario.setBackground(new Color(255, 255, 255));
                senhavazio = false;
            }
            if (jComboBox_Cargo.getSelectedIndex() == 0) {
                jComboBox_Cargo.setBackground(new Color(255, 209, 209));
                permissaovazio = true;
            } else {
                jComboBox_Cargo.setBackground(new Color(255, 255, 255));
                permissaovazio = false;
            }

            //FIM DO CÓDICO DE CHECAR E ALTERAR COR DOS CAMPOS VAZIOS **********


            //LOGICA PARA MANDA MENSAGEM DE ERRO (EXISTE CAMPOS VAZIOS)
            if (nomevazio == true || areavazio == true || loginavazio == true || senhavazio == true || permissaovazio == true) {
                java.awt.Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(null, "Por favor preencha os campos vazios.");
            } else if (nomevazio == false && areavazio == false && loginavazio == false && senhavazio == false
                    && permissaovazio == false) {


                JOptionPane.showMessageDialog(null, "Usuário cadastrado com sucesso!");

                clicou_ok = JOptionPane.showConfirmDialog(null, "Deseja continuar cadastrando?", "Confirmação", JOptionPane.YES_NO_OPTION);
                if (clicou_ok == JOptionPane.YES_OPTION) {
                    campoNome.setText("");
                    jFormattedTextField_Login.setText("");
                    jPasswordFieldSenhaUsuario.setText("");
                    jComboBox_Cargo.setSelectedIndex(0);
                    //FECHAR JANELA DE CADASTRO    
                } else if (clicou_ok == JOptionPane.NO_OPTION) {
                    this.dispose();
                }
            }
        } //FIM DO CÓDICO PARA CADASTRAR *********
    }//GEN-LAST:event_jButtonCadastroActionPerformed

    private void campoNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoNomeActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CadastroUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField campoNome;
    private javax.swing.JButton jButtonCadastro;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JComboBox jComboBox_Cargo;
    private javax.swing.JFormattedTextField jFormattedTextField_Login;
    private javax.swing.JLabel jLabelIMGBacgroundUsuario;
    private javax.swing.JLabel jLabelIMGCadastrarUsuario;
    private javax.swing.JLabel jLabelIMGLogo;
    private javax.swing.JLabel jLabelLogin;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelPermissao;
    private javax.swing.JLabel jLabelSenha;
    private javax.swing.JPasswordField jPasswordFieldSenhaUsuario;
    private javax.swing.JLabel textocadastro;
    // End of variables declaration//GEN-END:variables
}
